package phone.example.com.a36kr.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;

import phone.example.com.a36kr.R;
import phone.example.com.a36kr.nettool.NetTool;

/**
 * Created by dllo on 16/8/16.
 * 新闻页 轮播图 ViewPagerAdapter
 */
public class NewsViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener{

    ArrayList<String> mList;
    Context mContext;
    ViewPager viewPager;

    public NewsViewPagerAdapter(Context mContext) {
        this.mContext = mContext;
    }


    public void setmList(ArrayList<String> mList) {
        this.mList = mList;
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.carousel_news_im, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.carousel_news_im_im);
        NetTool netTool = new NetTool();
        netTool.getImage(mList.get(position % mList.size()), imageView);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
